package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * JDBC中的事务
 */
public class JDBCDemo11 {
    public static void main(String[] args) {
        /**
         * 事务默认是自动提交的
         * 每当我们执行一条DML操作就自动将事务提交了
         */

        try (Connection connection = DBUtil.getConnection();) {
            //设置事务不可提交
            connection.setAutoCommit(false);
            //支持设置事务的隔离级别
            connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

            String sql="update user set password where username=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,"123456");
            ps.setString(2,"王克晶");
            int num=ps.executeUpdate();
            System.out.println(num>0?"修改成功":"修改失败");

            connection.commit();//提交事务
            // connection.rollback();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}